import { resolve } from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import pxtoviewport from 'postcss-px-to-viewport'
import postcssPresetEnv from 'postcss-preset-env'

// https://vitejs.dev/config/
export default defineConfig((params) => {
  const platformName = process.env.platform
  // const { mode } = params
  return {
    define: {
      //  全局变量
      //...vars,
    },
    server: {
      host: '0.0.0.0', // 允许局域网访问
      // port: 3000,      // 你可以根据需要修改端口号
      proxy: {
        // "^/iflightapi/.*": {
        //   target: `https://wx.17u.cn`,
        //   changeOrigin: true,
        //   // rewrite: (path) => path.replace(/^\/mihawkapi/, ""),
        // },
      }
    },
    // envPrefix: "FX_", //  环境变量必须有一个前缀，在此申明
    plugins: [vue(), vueDevTools()],
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src'),
        '@platform': resolve(__dirname, `src/platforms/${platformName}/index`)
        // "@channelBase": resolve(__dirname, "src/channels/base"),
        // "@channelDir": resolve(__dirname, `src/channels/${channelName}/`),
        // "@channelRouter": resolve(__dirname, `src/channels/${channelName}/routers/index`),
        // "@modules": resolve(__dirname, "src/modules"),
        // "@book": resolve(__dirname, "src/modules/book"),
      }
    },
    build: {
      // sourcemap: false,
      // outDir: env.VITE_OUT_DIR,
      // emptyOutDir: true,
      // chunkSizeWarningLimit: 1500,
      // rollupOptions: {
      //     output: {
      //         assetFileNames: `assets/[name]-${time}.[ext]`,
      //         chunkFileNames: `assets/[name]-${time}.js`,
      //     },
      // },
    },
    css: {
      //配置vm适配
      postcss: {
        plugins: [
          pxtoviewport({
            viewportWidth: 375
          }),
          postcssPresetEnv() //  用于保证CSS代码在当前和旧版本的浏览器中兼容，将现代CSS特性转换为兼容的CSS
        ]
      },
      preprocessorOptions: {
        scss: {
          additionalData: `
            @import "@/assets/styles/variable.scss";
            @import "src/platforms/${platformName}/styles/variable.scss";
          `
        }
      }
    }
  }
})
